#include <stdio.h>
//#include "DbStruct.h"
#include "NetIOAPI.h"

#include	<sys/types.h>	/* basic system data types */
#include	<sys/socket.h>	/* basic socket definitions */
#include	<sys/time.h>	/* timeval{} for select() */
#include	<time.h>		/* timespec{} for pselect() */
#include	<netinet/in.h>	/* sockaddr_in{} and other Internet defns */
#include	<arpa/inet.h>	/* inet(3) functions */
#include	<errno.h>
#include	<fcntl.h>		/* for nonblocking */
#include	<netdb.h>
#include	<signal.h>
#include	<stdio.h>
#include	<stdlib.h>
#include	<string.h>
#include	<sys/stat.h>	/* for S_xxx file mode constants */
#include	<sys/uio.h>		/* for iovec{} and readv/writev */
#include	<unistd.h>
#include	<sys/wait.h>
#include	<sys/un.h>		/* for Unix domain sockets */
#include    <net/if.h>
#include    <sys/ioctl.h>
#include    <netinet/tcp.h>
#include    <fcntl.h>
#include    <stdint.h>



bool RecvDataCallBack(NETIOBuffer_S* pIOBuffer, const  sockaddr_in* pSockAddr, void* pUsr)
{
	//send_pk_t *wpk = calloc(1,sizeof(*wpk));
	//wpk->begin_pos = 0;
	//wpk->DataLen = pIOBuffer->usDataLen;
	//memcpy(wpk->Buffer,pIOBuffer->cBuffer,pIOBuffer->usDataLen);
	
	//NETIO_CloseConnect(lConnect);


	//long lConnect = (long)pUsr;

	
	NETIOBuffer_S send_buffer = {0};
	NETIO_GetNetIOBuffer(&send_buffer);
	memcpy(send_buffer.cBuffer,pIOBuffer->cBuffer,pIOBuffer->usDataLen);
	send_buffer.usDataLen = pIOBuffer->usDataLen;
	NETIO_SendTcpData(pUsr,&send_buffer);
	return true;
}

void DisConnectCallBack(void* pUsr)
{
	//printf("disconnect\n");
}

void FreeConnnectCallBack(void* pUsr)
{
	//printf("free\n");
}

bool ConnectCallBack(void* lTcpListen, void* lConnect, void* pUsr)
{
	//PRE_SOCKET_CONTEXT *c = (PRE_SOCKET_CONTEXT *)lConnect;
	//if(c)
	{
		printf("a new client\n");
		NETIO_SetConnRecvDataCB(lConnect,(void*)lConnect,RecvDataCallBack);
		NETIO_SetConnDisConnectCB(lConnect,(void*)lConnect,DisConnectCallBack);
		NETIO_SetConnFreeConnectCB(lConnect,(void*)lConnect,FreeConnnectCallBack);
	}
	return true;
}


int main(int argc,char **argv)
{
	NETIO_Initialize();
	NETIO_StartNetService();
	NETIO_BindTcpListen(argv[1],atoi(argv[2]),NULL,ConnectCallBack,NULL);
	getchar();
	return 0;
}